#include "httpclient.h"

#include <QTcpSocket>
#include <QString>
#include <QStringList>
#include <QDateTime>
#include <QTextStream>

#include <unistd.h>

HttpClient::HttpClient(QTcpSocket *socket, QObject *parent) : Client(socket, parent)
{
    connect(p_socket, SIGNAL(readyRead()), this, SLOT(processIncomingData()));
}


void HttpClient::processIncomingData(){
    v_lastUnprocessedData += p_socket->readAll();

    QStringList requests = QString(v_lastUnprocessedData).split(QRegExp("\n[\n\r]*\n"), QString::KeepEmptyParts);
    QString request;
    int i;
    for(i = 0; i < requests.count() - 1; i++)
    {
        request = requests.at(i);
        HttpRequest req(request);
        Q_EMIT requestReceived(req);
    }
    v_lastUnprocessedData = requests.last().toLatin1();
}

void HttpClient::sendRequest(HttpRequest request)
{
    p_socket->write(request.content().toLatin1());
}



